<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>TryLockMutex</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="4">TryLockMutex()</font></b></p>
<p><b>语法</b></p><blockquote>

Result = <font color="#3A3966"><b>TryLockMutex</b></font>(Mutex)</blockquote>

</blockquote>
<b>概要</b><br><blockquote>



Tries to lock the specified mutex. Unlike <a href="lockmutex.html">LockMutex()</a>, this function does not 
stop execution until the mutex is available. It returns immediately and the return-value indicates 
if the lock was successful or not. This is useful in situations were the thread should not wait 
for the mutex to be available but rather do other things in the meantime. 

 

</blockquote><p><b>参数</b></p><blockquote>
<style type="text/css">
table.parameters { border-spacing: 0px; border-style: none; border-collapse: collapse; }
table.parameters td { border-width: 1px; padding: 6px; border-style: solid; border-color: gray; vertical-align: top; font-family:Arial; font-size:10pt; }
</style>
<table width="90%" class="parameters">
<tr><td width="10%"><i>Mutex</i></td>
<td width="90%"> 
 
The mutex to lock. 

 

</td></tr>
</table>
</blockquote><p><b>返回值</b></p><blockquote>



Nonzero if the mutex was successfully locked, zero otherwise. 

 

</blockquote><p><b>备注</b></p><blockquote>



If the lock was successful, the <a href="unlockmutex.html">UnlockMutex()</a> function must be called 
to make the mutex available to other threads again. If this is not done, this could easily lead to a lockup situation. 

 

</blockquote><p><b>示例</b></p><blockquote>

<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">ThreadProcedure</font>(*Value)
    <b><font color="#3A3966">Shared</font></b> Mutex
    
    <b><font color="#3A3966">Repeat</font></b>
      <b><font color="#3A3966">If</font></b> <font color="#3A3966">TryLockMutex</font>(Mutex)
<font color="#3A3966">        PrintN</font>("Mutex successfully locked.")
        
<font color="#3A3966">        UnlockMutex</font>(Mutex)
        <b><font color="#3A3966">Break</font></b> <font color="#3A3966">; quit the loop and thread</font>
      <b><font color="#3A3966">Else</font></b>
<font color="#3A3966">        PrintN</font>("Still waiting for mutex access...")
<font color="#3A3966">        Delay</font>(200)
      <b><font color="#3A3966"></font></b>
    <b><font color="#3A3966">ForEver</font></b>
  <b><font color="#3A3966">EndProcedure</font></b>
  
<font color="#3A3966">  OpenConsole</font>()
  
  Mutex =<font color="#3A3966"> CreateMutex</font>()
<font color="#3A3966">  LockMutex</font>(Mutex) <font color="#3A3966">; main program has the mutex locked at first</font>
  Thread =<font color="#3A3966"> CreateThread</font>(@ThreadProcedure(), 0)
  
<font color="#3A3966">  Delay</font>(4000)
<font color="#3A3966">  UnlockMutex</font>(Mutex) <font color="#3A3966">; now release the mutex, so the thread can get it</font>
  
<font color="#3A3966">  Input</font>()
</font></pre>

</blockquote><p><b>参阅</b></p><blockquote>

<a href="unlockmutex.html">UnlockMutex()</a> 

</Blockquote><p><b>已支持操作系统 </b><Blockquote>所有</Blockquote></p><center>&lt;- <a href=threadpriority.html>ThreadPriority()</a> - <a href="index.html">Thread Index</a> - <a href="trysemaphore.html">TrySemaphore()</a> -&gt;<br><br>

</body></html>